﻿#region File Header
/***********************************************************************
 * Copyright © 2013 Beals Software
 * All Rights Reserved
************************************************************************
Author: Donald Beals
Date: February 4th, 2013
Description: TODO: Write a description of this file here.
****************************** Change Log ******************************
02.04.13 - Created initial file. (dbeals)
***********************************************************************/
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameJoltServices.Requests;
using GameJolt.Async;
using GameJolt;
using System.IO;
#endregion

namespace GameJoltServices.News
{
	/// <summary>
	/// 
	/// </summary>
	public sealed class NewsService : ServiceBase
	{
		#region Variables
		#endregion

		#region Properties
		#endregion

		#region Constructors
		public NewsService(ServicesManager manager)
			: base(manager)
		{
		}
		#endregion

		#region Methods
		private NewsItem[] GetNewsFromData(string data)
		{
			var output = new List<News.NewsItem>();
			using(var stream = new MemoryStream(Convert.FromBase64String(data)))
			{
				using(var reader = new BinaryReader(stream))
				{
					while(stream.Position < stream.Length)
					{
						output.Add(new News.NewsItem
						{
							Title = reader.ReadString(),
							Content = reader.ReadString(),
							DateTimePosted = new DateTime(reader.ReadInt64()),
						});
					}
				}
			}
			return output.ToArray();
		}

		private string NewsToData(NewsItem[] newsItems)
		{
			using(var stream = new MemoryStream())
			{
				using(var writer = new BinaryWriter(stream))
				{
					foreach(var item in newsItems)
					{
						writer.Write(item.Title);
						writer.Write(item.Content);
						writer.Write(item.DateTimePosted.Ticks);
					}

					return Convert.ToBase64String(stream.ToArray());
				}
			}
		}

		public IAsyncResult BeginGetNews()
		{
			return DataStore.BeginGetByKey(Manager.GameID, Manager.PrivateKey, null, null, "GameNews", null, null);
		}

		public NewsItem[] EndGetNews(IAsyncResult result)
		{
			var dataStore = DataStore.EndGetByKey(result);
			if(dataStore == null)
				return new NewsItem[0];
			return GetNewsFromData(dataStore.Data);
		}

		public NewsItem[] GetNews()
		{
			var dataStore = DataStore.GetByKey(Manager.GameID, Manager.PrivateKey, null, null, "GameNews");
			if(dataStore == null)
				return new NewsItem[0];
			return GetNewsFromData(dataStore.Data);
		}

		public IAsyncResult GetNews(Action<NewsItem[]> callback)
		{
			return DataStore.BeginGetByKey(Manager.GameID, Manager.PrivateKey, null, null, "GameNews", (result) => callback(EndGetNews(result)));
		}

		public IAsyncResult BeginSetNews(NewsItem[] newsItems)
		{
			return DataStore.BeginSetByKey(Manager.GameID, Manager.PrivateKey, null, null, "GameNews", NewsToData(newsItems), null, null);
		}

		public bool EndSetNews(IAsyncResult result)
		{
			return GameJolt.RequestHelper.WasSuccessful(DataStore.EndSetByKey(result));
		}

		public bool SetNews(NewsItem[] newsItems)
		{
			return GameJolt.RequestHelper.WasSuccessful(DataStore.SetByKey(Manager.GameID, Manager.PrivateKey, null, null, "GameNews", NewsToData(newsItems)));
		}

		public IAsyncResult SetNews(NewsItem[] newsItems, Action<bool> callback)
		{
			return DataStore.BeginSetByKey(Manager.GameID, Manager.PrivateKey, null, null, "GameNews", NewsToData(newsItems), (result) => callback(EndSetNews(result)));
		}
		#endregion
	}
}
